yjx.controller('couponController', ['$scope', '$timeout', '$fetch', 'modalService','ipCookie','$state', 'htmlHandler',function ($scope, $timeout, $fetch, modalService,ipCookie,$state,htmlHandler) {
  var api = '/user/coupon/me';
  $scope.couponStr= [];
  function getCoupons(type) {
    $fetch.fetch(
      api,
      {
        params: { state: type },
        cache: false
      }
    ).then(function (data) {
      $timeout(function () {
        if (type == 0) {
          $scope.couponList1 = data;
        } else if (type == 1) {
          $scope.couponList3 = data;
        }else if (type == 2){
          $scope.couponList2 = data;
        }
      })
    }).catch(function (e) {
      modalService.mainMsgError(e.msg);
    })
  }

  getCoupons(0);
  getCoupons(1);
  getCoupons(2);

  $scope.couponSelect = function(){
    if($scope.couponStr.length==0){
      return;
    }
    $fetch.fetch(
      '/user/coupon/receive',
      {
       params:{ couponTempletIds:$scope.couponStr.join(',')},
       type:'POST'
      }
    ).then(function(data){
      modalService.mainMsgOk("已领取")
      $timeout(function(){
        getCoupons(0);
        getCoupons(2);
        $scope.tabSelect($scope.tabList[0],0);
        $scope.couponStr=[];
      })
    }).catch(function(e){
      modalService.mainMsgError(e.msg);
    })
  }
  /**swiper */
  var tabList, couponSwiper;
  var width = document.body.offsetWidth;
  $scope.tabList = [{ id: 1, name: '已领取' }, { id: 2, name: '可领取' }, { id: 3, name: '已过期' }];
  $scope.curTab = $scope.tabList[0];
  function buildSwiper() {
    tabList = new Swiper('#tabList', {
      slidesPerView: 3,
      slideToClickedSlide: true,
    });
    couponSwiper = new Swiper('#couponSwiper', {
      direction: 'horizontal',
      // noSwiping : true,
      slider: width,
      on: {
        slideNextTransitionStart: function () {
          tabList.slideTo(couponSwiper.activeIndex)
          $timeout(function () {
            $scope.curTab = $scope.tabList[couponSwiper.activeIndex];
          })
        },
        slidePrevTransitionStart: function () {
          tabList.slideTo(couponSwiper.activeIndex - 1)
          $timeout(function () {
            $scope.curTab = $scope.tabList[couponSwiper.activeIndex];
          })
        }
      }
    })
  }
  $scope.tabSelect = function (item, index) {
    $scope.curTab = item;
    couponSwiper.slideTo(index, 500);
  }
  $timeout(function () { buildSwiper(); }, 500)

  /**选择单项item */
  $scope.checkIt = function (item) {
    item.isCheck = !item.isCheck;
    if(item.isCheck){
      $scope.couponStr.push(item.couponTempletId);
    }else{
      for(var i =0;i<$scope.couponStr.length;i++){
        if(item.couponTempletId == $scope.couponStr[i]){
          $scope.couponStr.splice(i,1);
          break;
        }
      }
    } 
  }

  /**去详情 */
  $scope.goDetail = function(item){
     item.describe = htmlHandler.fontsizeHandler(item.describe);
      ipCookie('selectCoupon',item,{expires: 1, domain: DOMAIN, path: PATH });
      $state.go('couponDetail');

  }
}])